package com.general.app.answer.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.transaction.annotation.Transactional;

import com.general.app.answer.dao.IAnswerDao;
import com.general.app.answer.dao.entity.Answer;
import com.general.app.answer.service.IAnswerService;
import com.general.common.service.impl.BaseServiceImpl;

public class AnswerServiceImpl extends BaseServiceImpl<Answer, IAnswerDao> 
implements IAnswerService{

	private final static String SPLIT_CHAR = "_";
	private final static String TEXT_OPTION = "0";
	
	/*
	 * (non-Javadoc)
	 * @see com.general.app.answer.service.IAnswerService#saveTextAnswer(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Transactional
	public boolean saveTextAnswer(String id, String relation, String qId, String answer) {
		
		String[] infoArr = id.split(SPLIT_CHAR);
		String sendId = infoArr[0];
		String surveyId = infoArr[1];
		String userId = infoArr[2];
		String assUserId = infoArr[3];
		
		Map<String,String> map = new HashMap<String, String>();
		map.put("sendId", sendId);
		map.put("surveyId", surveyId);
		map.put("userId", userId);
		map.put("assUserId", assUserId);
		map.put("questionId", qId);
		map.put("answer", answer);
		map.put("relation", relation);
		map.put("optionId", TEXT_OPTION);
		
		dao.delUserAnswer(map);
		return dao.insertTextAnswer(map) > 0;
	}


	/*
	 * (non-Javadoc)
	 * @see com.general.app.answer.service.IAnswerService#saveSingleQuestion(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Transactional
	public boolean saveSingleQuestion(String id, String relation, String qId,
			String oId, String score) {
		String[] infoArr = id.split(SPLIT_CHAR);
		String sendId = infoArr[0];
		String surveyId = infoArr[1];
		String userId = infoArr[2];
		String assUserId = infoArr[3];
		
		Map<String,String> map = new HashMap<String, String>();
		map.put("sendId", sendId);
		map.put("surveyId", surveyId);
		map.put("userId", userId);
		map.put("assUserId", assUserId);
		map.put("questionId", qId);
		map.put("score", score);
		map.put("relation", relation);
		
		dao.delUserAnswer(map);
		map.put("optionId", oId);
		return dao.insertSelectAnswer(map) > 0;
	}


	@Override
	public boolean delMultipleAnswer(String id, String relation, String qId,
			String oId) {
		String[] infoArr = id.split(SPLIT_CHAR);
		String sendId = infoArr[0];
		String surveyId = infoArr[1];
		String userId = infoArr[2];
		String assUserId = infoArr[3];
		
		Map<String,String> map = new HashMap<String, String>();
		map.put("sendId", sendId);
		map.put("surveyId", surveyId);
		map.put("userId", userId);
		map.put("assUserId", assUserId);
		map.put("questionId", qId);
		map.put("optionId", oId);
		map.put("relation", relation);
		
		return dao.delUserAnswer(map) > 0;
	}


	@Override
	public boolean saveMultipleAnswer(String id, String relation, String qId,
			String oId, String score) {
		String[] infoArr = id.split(SPLIT_CHAR);
		String sendId = infoArr[0];
		String surveyId = infoArr[1];
		String userId = infoArr[2];
		String assUserId = infoArr[3];
		
		Map<String,String> map = new HashMap<String, String>();
		map.put("sendId", sendId);
		map.put("surveyId", surveyId);
		map.put("userId", userId);
		map.put("assUserId", assUserId);
		map.put("questionId", qId);
		map.put("optionId", oId);
		map.put("relation", relation);
		map.put("score", score);
		return dao.insertSelectAnswer(map) > 0;
	}

	
}
